package dart

class Attempt {
	Classification classification;
	Integer type
	Integer value
	Integer multiplier
	Float harshPenalty

	static belongsTo = [round: Round]
	static transients = ['score']
	static constraints = {
		type(range: 0..2)
		value(nullable: true)
		multiplier(nullable: true)
		harshPenalty(nullable: true)
	}
	static Integer PENALTY = 0;
	static Integer MISS = 1;
	static Integer SUCCESS = 2;

	Float getScore() {
		Float result;
		Boolean useScore = round.playerGame.game.harshAndMainPenalties;
		switch (type) {
			case PENALTY:
				Float score = value.abs() * classification.penaltyMultiplier;
				result = -(useScore && score > harshPenalty ? score : harshPenalty);
				break;
			case MISS:
				result = 0.0
				break
			case SUCCESS:
				result = classification.scoreMultiplier * value * multiplier;
				break
			default:
				throw new BindException("Type is not a valid type (value = $type");
		}
		println ("Attempt.getScore() == ${Util.roundToValidScore(result)}")
		return Util.roundToValidScore(result);
	}

	static Attempt create(Map params, Game game) {
		Map properties = [classification: Classification.get(params.classification)]
		switch (params.multiplier as Integer) {
			case -1:
				properties.type = PENALTY
				properties.value = params.value
				break;
			case 0:
				properties.type = MISS
				break;
			case 1:
			case 2:
			case 3:
				properties.type = SUCCESS
				properties.value = params.value
				properties.multiplier = params.multiplier
				break;
			default:
				println "Woah! ${params.multiplier} isn't valid!"
		}
		return new Attempt(properties)
	}

}